<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>文章添加</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <link rel="stylesheet" type="text/css" th:href="@{/js/layui/css/layui.css}" media="all">

    <script type="text/javascript" th:src="@{/UEditor/ueditor.config.js}" media="all"></script>
    <script type="text/javascript" th:src="@{/UEditor/ueditor.all.js}" media="all"></script>
    <script type="text/javascript" th:src="@{/UEditor/lang/zh-cn/zh-cn.js}" media="all"></script>
    <style>
        .childrenBody{
            padding: 20px;
        }
    </style>
</head>
<body class="childrenBody">
<form class="layui-form" lay-filter="form">
    <input class="layui-input" name="id" id="id" type="hidden">
    <div class="layui-form-item">
        <label class="layui-form-label">标题</label>
        <div class="layui-input-block">
            <input class="layui-input" name="title" lay-verify="title" placeholder="请输入标题" type="text">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">列表图片</label>
        <div class="layui-input-block">
            <input class="layui-input" name="minImage" placeholder="列表图片" type="text">
        </div>

        <div class="layui-upload">
            <button type="button" class="layui-btn" id="test1">上传</button>
            <blockquote class="layui-elem-quote layui-quote-nm" style="margin-top: 10px;">
                预览图：
                <div class="layui-upload-list" id="demo1"></div>
            </blockquote>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">详情图片</label>
        <div class="layui-input-block">
            <input class="layui-input" name="maxImage" placeholder="列表图片" type="text">
        </div>

        <div class="layui-upload">
            <button type="button" class="layui-btn" id="test2">上传</button>
            <blockquote class="layui-elem-quote layui-quote-nm" style="margin-top: 10px;">
                预览图：
                <div class="layui-upload-list" id="demo2"></div>
            </blockquote>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">内容</label>
        <div class="layui-input-block">
            <textarea placeholder="请输入内容" style="height: 400px;width: 100%;"
                      class="layui-textarea" id="container" name="details">
            </textarea>
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn" lay-submit lay-filter="saveContainer">立即提交</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
    </div>
</form>
<script th:src="@{/js/layui/layui.js}"></script>

<script type="text/javascript" th:inline="javascript">
    layui.use(['form', "layer",'upload'], function () {
        // 操作对象
        var form = layui.form
            , upload = layui.upload
            , layer = layui.layer
            , $ = layui.jquery;

        upload.render({
            elem: '#test1' //绑定积分素
            , url: [[${basePath}]] + '/upload/oss'
            ,before: function(obj){
                obj.preview(function(index, file, result){
                    $('#demo1').html('<img style="width: 50px;height: 50px;" src="'+ result +'" alt="'+ file.name +'" class="layui-upload-img">')
                });
            }
            , done: function (res) {
                $("input[name='minImage']").val('' + res.data);
            }
        });
        upload.render({
            elem: '#test2' //绑定积分素
            , url: [[${basePath}]] + '/upload/oss'
            ,before: function(obj){
                obj.preview(function(index, file, result){
                    $('#demo2').html('<img style="width: 50px;height: 50px;" src="'+ result +'" alt="'+ file.name +'" class="layui-upload-img">')
                });
            }
            , done: function (res) {
                $("input[name='maxImage']").val('' + res.data);
            }
        });

        var knowledge = [[${knowledge}]];
        //回显表格数据
        if (knowledge != null) {
            form.val("form", {
                "id": knowledge.id
                ,"title": knowledge.title
                ,"minImage": knowledge.minImage
                ,"maxImage": knowledge.maxImage
                ,"title": knowledge.title
                ,"details": knowledge.details
            });
            $('#demo1').html('<img style="width: 50px;height: 50px;" src="'+ knowledge.minImage +'" alt="'+ knowledge.title +'" class="layui-upload-img">')
            $('#demo2').html('<img style="width: 50px;height: 50px;" src="'+ knowledge.maxImage +'" alt="'+ knowledge.title +'" class="layui-upload-img">')
        }

        // 实例化编辑器
        var ue = UE.getEditor('container', {
            // 其他配置参数
            // serverUrl: '/upload/oss' // 后端上传接口的URL
        });



        //监听提交
        form.on('submit(saveContainer)', function (data) {
            $.ajax({
                url: [[${basePath}]] + "/carbon/knowledge/saveKnowledge", data: data.field, sync: "post", success: function (data) {
                    console.log(data);
                    if (data.success) {
                        layer.msg(data.msg, {icon: 1});
                        setTimeout(function () {
                            var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                            parent.location.href = [[${basePath}]] + "/carbon/knowledge/index";
                            parent.layer.close(index);
                        }, 1500);
                    } else {
                        layer.msg(data.msg, {icon: 5});
                    }
                }
            });

            return false;
        });

    });
</script>
</body>
</html>